/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.serverpackets;

import l1j.server.server.model.Instance.L1ItemInstance;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_AddItem extends ServerBasePacket {

	private static final String S_ADD_ITEM = "[S] S_AddItem";

	/**
	 * インベントリにアイテムを一つ追加する。
	 */
	public S_AddItem(L1ItemInstance item) {
		writeC(0);
		writeD(item.getId());
		writeH(item.getItem().getItemDescId());
		writeC(item.getItem().getUseType());
		writeC(0);
		writeH(item.get_gfxid());
		writeC(item.getBless());
		writeD(item.getCount());
		int state = 0;
        if (item.isIdentified()) {
                state |= 1;
                if (!item.getItem().isTradable()) {
                        state |= 2;
                }
                if (item.getItem().isCantDelete()) {
                        state |= 4;
                }
                if (item.isArmor() || item.isWeapon()) {
                        if (item.getItem().get_safeenchant() < 0) {
                                state |= 8;
                        }
                }
        }
        if (item.getBless() > 128) {
                state = 32;
                state |= 2;
                state |= 4;
                state |= 8;
                if (item.isIdentified()) {
                        state |= 1;
                }
        }
        writeC(state);
		writeS(item.getViewName());
		if (!item.isIdentified()) {
			// 未鑑定の場合ステータスを送る必要はない
			writeC(0);
		} else {
			byte[] status = item.getStatusBytes();
			writeC(status.length);
			for (byte b : status) {
				writeC(b);
			}
		}
		writeC(0x0a);
        writeH(0);
        writeD(0);
        writeD(0);
	}

	@Override
	public byte[] getContent() {
		byte[] b = getBytes();
		b[0] = (byte) opcodes.S_OPCODE_ADDITEM;
		return b;
	}

	@Override
	public String getType() {
		return S_ADD_ITEM;
	}
}
